package com.fyzk.utils.jc;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by miao on 2017/8/16.
 */

public class CombinationUtil {

    ArrayList<String> str;

    public CombinationUtil(ArrayList<String> str) {
        this.str = str;
    }


    public void combiantion(char chs[]) {
        if (chs == null || chs.length == 0) {
            return;
        }
        List<Character> list = new ArrayList();
        for (int i = 1; i <= chs.length; i++) {
            combine(chs, 0, i , list);
        }
    }

    private List<String[]> list_all = new ArrayList<>();


    public List<String[]> getCombineResult() {
//        char chs[] = new char[str.size()];
        String chartag = "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^-`abcde";
        String char_ = chartag.substring(0,str.size());
        combiantion(char_.toCharArray());
        return list_all;
    }


    //从字符数组中第begin个字符开始挑选number个字符加入list中
    public void combine(char[] cs, int begin, int number, List<Character> list) {
        if (number == 0) {
            String[] characters = new String[list.size()];
            int x = 0;
            for (Character character : list) {
                try {
                    characters[x] = str.get(character.charValue() - 48);//48为字符'0'的十进制对应值
                } catch (IndexOutOfBoundsException e) {
//                    LogUtil.e("characters.length:>" + characters.length + "}{itemCount:>" +
//                            itemCount + "}{x:>" + x + "}{character:>" + character);
                }
                x++;
            }
            if(characters!=null&&characters.length>0){
                list_all.add(characters);
            }
            return;
        }
        if (begin == cs.length) {
            return;
        }
        list.add(cs[begin]);
        combine(cs, begin + 1, number - 1, list);
        list.remove((Character) cs[begin]);
        combine(cs, begin + 1, number, list);
    }

}
